﻿using System.Data.Entity;
namespace XStudio.XCRF.Data.Tools
{
    /// <summary>
    /// XCRF数据库管理器
    /// </summary>
    public class XCRFDatabaseManager
    {
        #region 核心

        /// <summary>
        /// 构造方法新的数据库管理器
        /// </summary>
        public XCRFDatabaseManager()
        {
            
        }

        #endregion 核心

        #region 公开方法

        /// <summary>
        /// 创建数据库
        /// </summary>
        public static void BuildDatabase()
        {
            Database.SetInitializer(new XCRFInitializer());
            using (XCRFContext context = new XCRFContext() { DebugModeOn = true })
            {
                context.Database.Initialize(force: false);
            }
        }

        /// <summary>
        /// 删除数据库
        /// </summary>
        public static void DropDatabase()
        {
            using (XCRFContext context = new XCRFContext())
            {
                //context.Database.ExecuteSqlCommand("ALTER DATABASE XCRF SET SINGLE_USER with ROLLBACK IMMEDIATE");
                if (context.Database.Exists())
                    context.Database.Delete();
            }
        }
        #endregion 公开方法
    }
}
